package zlog

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

const (
	logTmFmt = "2006-01-02 15:04:05.000000"
)

var (
	_logger          *zap.Logger
	_logConf         LogConfig
	_fileEncoder     zapcore.Encoder
	_fileWriteSyncer zapcore.WriteSyncer
	_consoleEncoder  zapcore.Encoder
)

type LogConfig struct {
	Output     string
	FilePath   string
	FileLevel  int
	ServerType string
	ServerID   int
}

func Init(conf LogConfig) {
	_logConf = conf
	_fileEncoder = GetFileEncoder()
	_consoleEncoder = GetConsoleEncoder()
	_fileWriteSyncer = GetWriteSyncer()
	FileLevel := zapcore.Level(_logConf.FileLevel)
	var newCore zapcore.Core
	if _logConf.Output == "file" {
		newCore = zapcore.NewTee(
			zapcore.NewCore(_fileEncoder, _fileWriteSyncer, FileLevel), // 写入文件
		)
	} else if _logConf.Output == "both" {
		newCore = zapcore.NewTee(
			zapcore.NewCore(_fileEncoder, _fileWriteSyncer, FileLevel),                    // 写入文件
			zapcore.NewCore(_consoleEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel), // 写入控制台
		)
	} else {
		newCore = zapcore.NewTee(
			zapcore.NewCore(_consoleEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel), // 写入控制台
		)
	}

	_logger = zap.New(newCore, zap.AddCaller())

	Debug("Debug", zap.Int("", int(zapcore.DebugLevel)))
	Info("Info", zap.Int("", int(zapcore.InfoLevel)))
	Warn("Warn", zap.Int("", int(zapcore.WarnLevel)))
	Error("Error", zap.Int("", int(zapcore.ErrorLevel)))
	DPanic("DPanic", zap.Int("", int(zapcore.DPanicLevel)))
	//Panic("Panic", zap.Int("", int(zapcore.PanicLevel)))
	//Fatal("Fatal", zap.Int("", int(zapcore.FatalLevel)))
}
